package org.yunshang.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.yunshang.customer.domain.bo.TCustomerBo;
import org.yunshang.customer.domain.vo.TCustomerVo;
import org.yunshang.customer.service.ITCustomerService;
import org.yunshang.common.core.domain.R;
import org.yunshang.common.core.validate.AddGroup;
import org.yunshang.common.core.validate.EditGroup;
import org.yunshang.common.core.validate.QueryGroup;
import org.yunshang.common.excel.utils.ExcelUtil;
import org.yunshang.common.idempotent.annotation.RepeatSubmit;
import org.yunshang.common.log.annotation.Log;
import org.yunshang.common.log.enums.BusinessType;
import org.yunshang.common.mybatis.core.page.PageQuery;
import org.yunshang.common.mybatis.core.page.TableDataInfo;
import org.yunshang.common.web.core.BaseController;
import org.yunshang.system.service.ISysOssService;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import java.util.List;

/**
 * 客户信息
 *
 * @author wangzr
 * @date 2024-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/customer")
public class TCustomerController extends BaseController {

    private final ITCustomerService tCustomerService;

    private final ISysOssService ossService;

    /**
     * 查询客户信息列表
     */
    @SaCheckPermission("customer:customer:list")
    @GetMapping("/list")
    public TableDataInfo<TCustomerVo> list(@Validated(QueryGroup.class) TCustomerBo bo, PageQuery pageQuery) {
        return tCustomerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出客户信息列表
     */
    @SaCheckPermission("customer:customer:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TCustomerBo bo, HttpServletResponse response) {
        List<TCustomerVo> list = tCustomerService.queryList(bo);
        ExcelUtil.exportExcel(list, "客户信息", TCustomerVo.class, response);
    }

    /**
     * 获取客户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("customer:customer:query")
    @GetMapping("/{id}")
    public R<TCustomerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tCustomerService.queryById(id));
    }

    /**
     * 新增客户信息
     */
    @SaCheckPermission("customer:customer:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TCustomerBo bo) {
        return toAjax(tCustomerService.insertByBo(bo));
    }

    /**
     * 修改客户信息
     */
    @SaCheckPermission("customer:customer:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TCustomerBo bo) {
        return toAjax(tCustomerService.updateByBo(bo));
    }

    /**
     * 删除客户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("customer:customer:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tCustomerService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 客户log上传
     */
//    @Log(title = "客户log上传", businessType = BusinessType.UPDATE)
//    @PostMapping(value = "/uploadLog", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public R<LogVo> uploadLog(@RequestPart("avatarfile") MultipartFile avatarfile, Long cusId) {
//        if (!avatarfile.isEmpty()) {
//            String extension = FileUtil.extName(avatarfile.getOriginalFilename());
//            if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
//                return R.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
//            }
//            SysOssVo oss = ossService.upload(avatarfile);
//            String log = oss.getUrl();
//            if (tCustomerService.updateLog(cusId, oss.getOssId())) {
//                LogVo logVo = new LogVo();
//                logVo.setImgUrl(log);
//                return R.ok(logVo);
//            }
//        }
//        return R.fail("上传图片异常，请联系管理员");
//    }

}
